﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NCP.Recruiting.WebApp.Models;
using NCP.Recruiting.ApplicationServices.Messaging.Settings;
using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.ViewModels;

namespace NCP.Recruiting.WebApp.Controllers {
    public class TagController : AuthorizedController {
        private readonly ICompanyAppService _companyAppService;
        private readonly ISettingAppService _settingAppService;

        public TagController(ICompanyAppService companyAppService, ISettingAppService settingAppService)
            : base(companyAppService) {
            this._companyAppService = companyAppService;
            this._settingAppService = settingAppService;
        }


        public ActionResult List() {
            var request = new FindTagsRequest {
                PageIndex = 0,
                PageCount = 10,
                CompanyId = CurrentUserId,
            };

            var response = new FindTagsResponse();
            response = _settingAppService.FindTags(request);

            return View(response.Tags);
        }

        public ActionResult Add() {
            return View(new TagFormModel());
        }

        [HttpPost]
        public ActionResult Add(TagFormModel tagForm) {
            if (ModelState.IsValid) {
                tagForm.Deleted = false;
                tagForm.CompanyId = this.CurrentUserId;
                tagForm.TagCount = 0;

                CreateTagRequest request = new CreateTagRequest();
                request.TagProperties = tagForm;

                var response = _settingAppService.CreateTag(request);

                return RedirectToAction("List");
            }

            return View();
        }

        public ActionResult Edit(Guid Id) {
            var response = _settingAppService.GetTag(new GetTagRequest(Id));

            return View(ConstructForm(response.TagProperties));
        }

        [HttpPost]
        public ActionResult Edit(TagFormModel tagForm) {
            if (TryUpdateTag(tagForm)) {
                return RedirectToAction("List");
            }

            var fm = ConstructForm(_settingAppService.GetTag(new GetTagRequest(tagForm.Id)).TagProperties);
            return View(fm);
        }

        private bool TryUpdateTag(TagFormModel tagForm) {
            if (!ModelState.IsValid)
                return false;

            if (ModelState.IsValid) {
                var request = new UpdateTagRequest(tagForm.Id);
                request.TagProperties = tagForm;
                var response = _settingAppService.UpdateTag(request);

                return true;
            }
            return false;
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id) {
            var request = new DeleteTagRequest(id);
            var response = _settingAppService.DeleteTag(request);

            if (response.Exception == null) {
                RedirectToAction("List");
            }

            return RedirectToAction("List");
        }

        private TagFormModel ConstructForm(TagViewModel model) {
            return new TagFormModel {
                Id = model.Id,
                TagCount = model.TagCount,
                CompanyId = model.CompanyId,
                Selected = model.Selected,
                TagName = model.TagName,
                Deleted = model.Deleted
            };
        }

    }
}
